<template>
  <div>
    <!-- 搜索栏 -->
    <el-form :inline="true" :model="formInline" class="search-container">
      <el-form-item label="搜索：" class="search-label">
        <el-input
          v-model="formInline.storeName"
          placeholder="请输入店铺名称"
          prefix-icon="el-icon-search"
          class="search-input"
        ></el-input>
      </el-form-item>

      <el-form-item class="btn-group">
        <el-upload
          class="avatar-uploader"
          :action="uploadPicUrl"
          :show-file-list="false"
          :on-success="handleAvatarSuccess1"
          :before-upload="beforeAvatarUpload"
        >
          <el-button
            type="warning"
            icon="el-icon-picture"
            class="verify-btn"
          >图片核验</el-button>
        </el-upload>
      </el-form-item>

      <el-form-item class="btn-group">
        <el-button
          type="primary"
          icon="el-icon-search"
          class="search-btn"
          @click="getData"
        >搜索</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="24">
      <el-col :span="6" v-for="(item, index) in listData" :key="index">
        <div class="store-card" @click="approval(index, item)">
          <div class="store-image-wrapper">
            <img :src="showUrl+item.imgPath" class="store-image" alt=""/>
            <div class="store-overlay">
              <span>点击查看详情</span>
            </div>
          </div>
          <div class="store-info">
            <span class="store-name">店名：{{ item.storeName }}</span>
          </div>
        </div>
      </el-col>
    </el-row>

    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>

    <!-- 审批界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisibleApproval" width="50%" @click="closeDialogApproval">
      <div class="dialog-content">
        <!-- 店铺信息卡片 -->
        <div class="info-card">
          <h3 class="card-title">店铺信息</h3>
          <el-form label-width="120px" :model="editForm" :rules="rulesApproval" ref="editForm" class="compact-form">
            <el-form-item label="店铺图片" prop="imgPath">
              <img v-if="editForm.imgPath" :src="showUrl + editForm.imgPath" class="store-img">
            </el-form-item>
            <div class="form-content">
              <el-form-item label="店铺名称" prop="storeName">
                {{ editForm.storeName }}
              </el-form-item>
              <el-form-item label="店铺类型" prop="storeType">
                {{
                  editForm.storeType === '1' ? "服饰" :
                    editForm.storeType === "2" ? "家居" :
                      editForm.storeType === "3" ? "数码" :
                        editForm.storeType === "4" ? "美妆" :
                          editForm.storeType === "5" ? "餐饮" :
                            editForm.storeType === "6" ? "其他" : "未知"
                }}
              </el-form-item>
              <el-form-item label="店铺规模" prop="storeScale">
                {{
                  editForm.storeScale === '1' ? "小型店铺" :
                    editForm.storeScale === "2" ? "中型店铺" :
                      editForm.storeScale === "3" ? "大型店铺" : "未知"
                }}
              </el-form-item>
              <el-form-item label="业务范围" prop="business">
                {{ editForm.business }}
              </el-form-item>
              <el-form-item label="店铺简介" prop="remark">
                {{ editForm.remark }}
              </el-form-item>
              <el-form-item label="店铺面积" prop="area">
                {{ editForm.area }}
              </el-form-item>
              <el-form-item label="店铺地址" prop="address">
                {{ editForm.districtName }}{{ editForm.streetName }}{{ editForm.address }}
              </el-form-item>
            </div>
          </el-form>
        </div>

        <!-- 评论区卡片 -->
        <div class="comment-card">
          <h3 class="card-title">评论区</h3>
          <div class="comment-list">
            <div v-for="(item, index) in messageList" :key="index" class="comment-item">
              <div class="comment-header">
                <span class="username">{{ item.userName }}</span>
                <span class="time">{{ item.createTime }}</span>
              </div>
              <div class="comment-body">
                {{ item.content }}
              </div>
              <div class="comment-footer">
            <span @click="reply(editForm, item)" class="reply-btn">
              <i class="el-icon-chat-line-square"></i> 回复
            </span>
            <span @click="deleteMessage(editForm, item)" class="reply-btn" v-if="userType == '1' || userId == item.userId">
              <i class="el-icon-chat-line-square"></i> 删除
            </span>
              </div>
            </div>
          </div>

          <!-- 评论输入框 -->
          <div class="comment-input">
            <el-input
              v-model="editForm.content"
              type="textarea"
              :rows="3"
              placeholder="写下你的评论..."
              resize="none"
            ></el-input>
          </div>
        </div>
      </div>

      <!-- 底部按钮 -->
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialogApproval">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" @click="approvalSubmit('editForm')">
          提交评论
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as api from '../../api/interface'
import Pagination from '../../components/Pagination'
import {queryAreaList} from "../../api/interface";

export default {
  data() {
    return {
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      editFormVisibleApproval: false, //控制编辑页面显示与隐藏
      addFormVisible: false,//控制添加页面显示与隐藏
      title: '',
      editForm: {},
      addForm: {},
      messageList: [],
      // rules表单验证
      rules: {
        storeName: [{required: true, message: '请输入店铺名称', trigger: 'blur'}],
        storeType: [{required: true, message: '请输入店铺类型', trigger: 'blur'}],
        storeScale: [{required: true, message: '请输入店铺规模', trigger: 'blur'}],
        // provinceId: [{required: true, message: '请输入所属省份', trigger: 'blur'}],
        // cityId: [{required: true, message: '请输入所属城市', trigger: 'blur'}],
        districtId: [{required: true, message: '请输入所属区县', trigger: 'blur'}],
        streetId: [{required: true, message: '请输入所属街道', trigger: 'blur'}],
        address: [{required: true, message: '请输入店铺地址', trigger: 'blur'}],
        imgPath: [{required: true, message: '请上传图片', trigger: 'blur'}],
      },
      // rules表单验证
      rulesApproval: {
        approvalStatus: [{required: true, message: '请选择审批状态', trigger: 'blur'}],
        approvalRemark: [{required: true, message: '请输入审批意见', trigger: 'blur'}],
      },
      //查询条件
      formInline: {
        page: 1, //当前页面
        limit: 20, //每页数量
        storeName: ''
      },
      // 删除部门
      seletedata: {
        ids: '',
        token: localStorage.getItem('logintoken')
      },
      userparm: [], //搜索权限
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 20,
        total: 20
      },
      userType: "",
      userId: "",
      userName: "",
      uploadFileUrl: api.uploadFileUrl,
      uploadPicUrl: api.uploadPicUrl,
      showUrl: api.showUrl,
      tempId: 1,
      storeTypeList: [
        {code: "", name: "请选择"},
        {code: "1", name: "服饰"},
        {code: "2", name: "家居"},
        {code: "3", name: "数码"},
        {code: "4", name: "美妆"},
        {code: "5", name: "餐饮"},
        {code: "6", name: "其他"}
      ],
      storeScaleList: [
        {code: "", name: "请选择"},
        {code: "1", name: "小型店铺"},
        {code: "2", name: "中型店铺"},
        {code: "3", name: "大型店铺"}
      ],
    }
  },
  // 注册组件
  components: {
    Pagination
  },
  /**
   * 页面初始化方法
   */
  created() {
    this.userType = localStorage.getItem("userType");
    this.userId = localStorage.getItem("userId");
    this.userName = localStorage.getItem("userName");
    console.log(this.userId)
    console.log(this.userName)
    this.getData()
  },
  methods: {
    formatStoreType: function (row, column) {
      return row.storeType == '1' ? "服饰" :
        row.storeType == "2" ? "家居" :
          row.storeType == "3" ? "数码" :
            row.storeType == "4" ? "美妆" :
              row.storeType == "5" ? "餐饮" :
                row.storeType == "6" ? "其他" :
                  "未知";
    },
    formatStoreScale: function (row, column) {
      return row.storeScale == '1' ? "小型店铺" :
        row.storeScale == "2" ? "中型店铺" :
          row.storeScale == "3" ? "大型店铺" :
            "未知";
    },
    formatApprovalStatus: function (row, column) {
      return row.approvalStatus == '1' ? "未审核" :
        row.approvalStatus == "2" ? "审核通过" :
          row.approvalStatus == "3" ? "审核未通过" :
            "未知";
    },
    formatApprovalRemark: function (row, column) {
      return row.approvalRemark && row.approvalRemark.length > 10 ? row.approvalRemark.substring(0, 10) + "..." : row.approvalRemark;
    },
    formatRemark: function (row, column) {
      return row.remark && row.remark.length > 10 ? row.remark.substring(0, 10) + "..." : row.remark;
    },
    // 获取列表数据
    getData() {
      this.loading = true
      let para = {
        current: this.formInline.page,
        size: this.formInline.limit,
        storeName: this.formInline.storeName
      }
      api.queryStoreByPage(para).then(data => {
        this.logining = false;
        if (data.message != "success") {
          this.logining = false;
          this.$message.error(data.message)
        } else {
          this.loading = false
          this.listData = data.returnPage.records
          // this.listData.forEach(food => {
          //   food.formula = "";
          //   if (food.foodFormulaList && food.foodFormulaList.length > 0) {
          //     food.foodFormulaList.forEach(item => {
          //       food.formula += item.materialName + "数量:" + item.amount + ",";
          //     })
          //     food.formula = food.formula.substring(0, food.formula.length - 1);
          //   }
          // })
          this.pageparm.currentPage = data.returnPage.current
          this.pageparm.pageSize = data.returnPage.size
          this.pageparm.total = data.returnPage.total
        }
      }).catch(e => {
        this.logining = false;
      });
    },
    queryProvince(querystring, callback) {
      let param = {
        "level": "1",
        "name": querystring
      }
      api.queryAreaList(param).then(data => {
        if (data.message != "success") {
          this.$message.error(data.message)
          return []
        } else {
          let list = data.returnList.map(item => {
            item.value = item.name
            return item;
          });
          callback(list);
        }
      })
    },
    handleSelectProvince(item, index) {
      this.editForm.provinceId = item.code;
      this.editForm.provinceName = item.name;
    },
    queryCity(querystring, callback) {
      let param = {
        "level": "2",
        "name": querystring,
        "parentCode": this.editForm.provinceId
      }
      api.queryAreaList(param).then(data => {
        if (data.message != "success") {
          this.$message.error(data.message)
          return []
        } else {
          let list = data.returnList.map(item => {
            item.value = item.name
            return item;
          });
          callback(list);
        }
      })
    },
    handleSelectCity(item, index) {
      this.editForm.cityId = item.code;
      this.editForm.cityName = item.name;
    },
    queryDistrict(querystring, callback) {
      let param = {
        "level": "3",
        "name": querystring,
        "parentCode": this.editForm.cityId
      }
      api.queryAreaList(param).then(data => {
        if (data.message != "success") {
          this.$message.error(data.message)
          return []
        } else {
          let list = data.returnList.map(item => {
            item.value = item.name
            return item;
          });
          callback(list);
        }
      })
    },
    handleSelectDistrict(item, index) {
      this.editForm.districtId = item.code;
      this.editForm.districtName = item.name;
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage
      this.formInline.limit = parm.pageSize
      this.getData()
    },
    //显示添加界面
    handleAdd: function (index, row) {
      this.editFormVisible = true
      this.title = '新增'
    },
    //显示编辑界面--编辑此行信息
    handleEdit: function (index, row) {
      this.editFormVisible = true
      this.title = '编辑'
      this.editForm = Object.assign({}, row)
    },
    // 关闭编辑
    closeDialog() {
      this.editFormVisible = false
    },
    // 关闭编辑
    closeDialogApproval() {
      this.editFormVisibleApproval = false
    },
    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          let para = Object.assign({}, this.editForm);
          this.$set(para, "userId", this.userId);
          this.$set(para, "userName", this.userName);
          console.log(this.userId, 123);
          console.log(this.userName, 456);
          console.log(para);
          this.loading = true;
          api.updateStore(para)
            .then(res => {
              this.loading = false;
              if (res.message == 'success') {
                this.$message({message: "提交成功", type: "success"});
                this.getData();
                this.editFormVisible = false;
              } else {
                this.$message({type: 'error', message: res.message})
              }
            })
            .catch(e => {
              // this.editFormVisible = false
              this.loading = false
            })
        } else {
          return false
        }
      })
    },
    // 审批
    approvalSubmit(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          let para = Object.assign({}, this.editForm);
          this.$set(para, "userId", this.userId);
          this.$set(para, "userName", this.userName);
          // para.approvalTime = new Date();
          // let userId = window.localStorage.getItem("userId");
          // let userName = window.localStorage.getItem("userName");
          // para.approvalUserId = userId;
          // para.approvalUserName = userName;
          // if (para.approvalStatus != '2' && para.approvalStatus != '3') {
          //   this.$message({type: 'error', message: "请选择审批状态"});
          //   return;
          // }
          this.loading = true;
          api.updateMessage(para)
            .then(res => {
              this.loading = false;
              if (res.message == 'success') {
                this.$message({message: "提交成功", type: "success"});
                this.getData();
                this.editFormVisibleApproval = false;
              } else {
                this.$message({type: 'error', message: res.message})
              }
            })
            .catch(e => {
              // this.editFormVisible = false
              this.loading = false
            })
        } else {
          return false
        }
      })
    },
    // 删除--此行
    handleDel(index, row) {
      this.$confirm('确定要删除吗?', '信息', {confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'})
        .then(() => {
          let para = {
            storeId: row.storeId
          }
          api.delStore(para)
            .then(res => {
              if (res.message == 'success') {
                this.$message({message: "删除成功", type: "success"});
                this.getData();
              } else {
                this.$message({type: 'error', message: res.message})
              }
            })
            .catch(e => {
              this.loading = false
            })
        });
    },
    handleAvatarSuccess(res, file) {
      this.editForm.imgPath = res.returnObj;
      this.$forceUpdate();
    },
    handleAvatarSuccess1(res, file) {
      if (res.message == 'success') {
        this.formInline.storeName = res.returnObj;
        this.getData();
      } else {
        this.$message({type: 'error', message: res.message})
      }
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传店铺图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传店铺图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    // appendMaterial() {
    //   this.editForm.foodFormulaList.push({
    //     "key": this.tempId++
    //   });
    // },
    // deleteAuto: function (index) {
    //   this.editForm.foodFormulaList.splice(index, 1);
    // },
    // querySearchAsync(queryString, callback) {
    //   let para = {materialName: queryString};
    //   // api.queryMaterial(para).then(res => {
    //   //     for (let i of res.returnList) {
    //   //         i.value = i.materialName;
    //   //     }
    //   //     var list = res.returnList;
    //   //     callback(list);
    //   // }).catch(() => {});
    // },
    // handleSelect(item, index) {
    //   this.editForm.foodFormulaList[index].materialName = item.materialName;
    //   this.editForm.foodFormulaList[index].materialId = item.materialId;
    //   this.editForm.foodFormulaList[index].unit = item.unit;
    // },
    //显示编辑界面--编辑此行信息
    approval: function (index, row) {
      this.editFormVisibleApproval = true
      this.title = '详情'
      this.editForm = Object.assign({}, row)

      let param = {storeId: row.storeId}
      api.queryMessage(param).then(res => {
        this.$set(this, "messageList", res.returnList);
      })
    },
    reply: function (editForm, message) {
      this.$set(editForm, "content", "回复 " + message.userName + ": ");
      editForm.responseMessageId = message.messageId;
      editForm.responseUserId = message.userId;
      editForm.responseUserName = message.userName;
    },
    deleteMessage: function (editForm, message) {
      api.delMessage({messageId: message.messageId}).then(res => {
        if(res.message == "success") {
          this.$message.success("删除成功");

          let param = {storeId: editForm.storeId}
          api.queryMessage(param).then(res => {
            this.$set(this, "messageList", res.returnList);
          })
        }else{
          this.$message.error("删除失败:" + message);
        }
      })
    }
  }
}
</script>

<style scoped>

.container {
  display: flex;
  flex-direction: column;
  align-items: center; /* 这会使得flex子项在主轴方向（此处为垂直方向）上居中 */
  text-align: center; /* 这会使得文本在水平方向上居中 */
  margin-top: 50px;
}

.container img {
  max-width: 100%; /* 确保图片不会超过其容器 */
}

.container p {
  margin-top: 20px; /* 为文字添加一些上边距，与图片下方对齐 */
}

.user-search {
  margin-top: 20px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.custom-margin {
  margin-bottom: 0px;
  margin-top: 0px; /* 调整这个值来控制行间距 */
}
.dialog-content {
  padding: 20px;
}

/* 卡片通用样式 */
.info-card,
.comment-card {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
  padding: 20px;
  margin-bottom: 20px;
}

.card-title {
  font-size: 18px;
  color: #303133;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 1px solid #EBEEF5;
}

/* 店铺信息样式 */
.store-img {
  width: 200px;
  height: 200px;
  object-fit: cover;
  border-radius: 4px;
}

.compact-form .el-form-item {
  margin-bottom: 12px;
}

.form-content {
  color: #606266;
}

/* 评论区样式 */
.comment-list {
  max-height: 400px;
  overflow-y: auto;
}

.comment-item {
  padding: 15px;
  border-bottom: 1px solid #EBEEF5;
}

.comment-item:last-child {
  border-bottom: none;
}

.comment-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.username {
  font-weight: 500;
  color: #409EFF;
}

.time {
  font-size: 12px;
  color: #909399;
}

.comment-body {
  color: #606266;
  line-height: 1.6;
  margin: 8px 0;
}

.comment-footer {
  display: flex;
  justify-content: flex-end;
}

.reply-btn {
  color: #409EFF;
  cursor: pointer;
  font-size: 13px;
}

.reply-btn:hover {
  color: #66b1ff;
}

.comment-input {
  margin-top: 20px;
}

/* 底部按钮样式 */
.dialog-footer {
  text-align: right;
  padding-top: 20px;
  border-top: 1px solid #EBEEF5;
}
.store-card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
  margin-bottom: 24px;
  transition: all 0.3s ease;
  cursor: pointer;
  overflow: hidden;
}

.store-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0,0,0,0.2);
}

.store-image-wrapper {
  position: relative;
  width: 100%;
  height: 300px;
  overflow: hidden;
}

.store-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.store-card:hover .store-image {
  transform: scale(1.05);
}

.store-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  opacity: 0;
  transition: opacity 0.3s ease;
  color: white;
  font-size: 16px;
}

.store-card:hover .store-overlay {
  opacity: 1;
}

.store-info {
  padding: 16px;
  text-align: center;
}

.store-name {
  font-size: 20px;
  color: cadetblue;
  font-weight: 500;
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.search-container {
  background: #f5f7fa;
  padding: 20px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
  margin-bottom: 20px;
  display: flex;
  align-items: center;
}

.search-label {
  font-weight: bold;
  color: #333;
  font-size: 16px;
  margin-right: 10px;
}

.search-label .el-form-item__label {
  font-size: 16px;
  line-height: 40px;
}

.search-input {
  width: 300px;
}

.search-input .el-input__inner {
  height: 40px;
  font-size: 16px;
  border: 1px solid #dcdfe6;
}

.btn-group {
  margin-left: 15px;
}

/* 统一按钮样式 */
.verify-btn,
.search-btn {
  height: 40px;
  font-size: 16px;
  padding: 0 25px;
  transition: all 0.3s;
}

.verify-btn {
  background: #e6a23c;
  border-color: #e6a23c;
  color: white;
}

.verify-btn:hover {
  background: #ebb563;
  border-color: #ebb563;
  opacity: 0.9;
}

.search-btn:hover {
  opacity: 0.9;
}

/* 图标样式 */
.el-icon-picture,
.el-icon-search {
  font-size: 16px;
  margin-right: 5px;
}

/* 表单项垂直对齐 */
.el-form-item {
  margin-bottom: 0;
  vertical-align: middle;
}

.el-form-item__content {
  line-height: 40px;
}

/* 输入框placeholder样式 */
.el-input__inner::placeholder {
  font-size: 14px;
  color: #999;
}

/* 确保上传组件样式正确 */
.avatar-uploader {
  display: inline-block;
}

</style>
